package com.ruoyi.citylife.controller;

import java.util.List;
import java.util.Arrays;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.citylife.domain.vo.CUserVo;
import com.ruoyi.citylife.domain.bo.CUserBo;
import com.ruoyi.citylife.service.ICUserService;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 微信用户Controller
 *
 * @author ruoyi
 * @date 2022-10-31
 */
@Validated
@Api(value = "微信用户控制器", tags = {"微信用户管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/citylife/user")
public class CUserController extends BaseController {

    private final ICUserService iCUserService;

    /**
     * 查询微信用户列表
     */
    @ApiOperation("查询微信用户列表")
    @PreAuthorize("@ss.hasPermi('citylife:user:list')")
    @GetMapping("/list")
    public TableDataInfo<CUserVo> list(@Validated CUserBo bo) {
        return iCUserService.queryPageList(bo);
    }

    /**
     * 导出微信用户列表
     */
    @ApiOperation("导出微信用户列表")
    @PreAuthorize("@ss.hasPermi('citylife:user:export')")
    @Log(title = "微信用户", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(@Validated CUserBo bo, HttpServletResponse response) {
        List<CUserVo> list = iCUserService.queryList(bo);
        ExcelUtil.exportExcel(list, "微信用户", CUserVo.class, response);
    }

    /**
     * 获取微信用户详细信息
     */
    @ApiOperation("获取微信用户详细信息")
    @PreAuthorize("@ss.hasPermi('citylife:user:query')")
    @GetMapping("/{id}")
    public AjaxResult<CUserVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("id") String id) {
        return AjaxResult.success(iCUserService.queryById(id));
    }

    /**
     * 新增微信用户
     */
    @ApiOperation("新增微信用户")
    @PreAuthorize("@ss.hasPermi('citylife:user:add')")
    @Log(title = "微信用户", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody CUserBo bo) {
        return toAjax(iCUserService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改微信用户
     */
    @ApiOperation("修改微信用户")
    @PreAuthorize("@ss.hasPermi('citylife:user:edit')")
    @Log(title = "微信用户", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody CUserBo bo) {
        return toAjax(iCUserService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除微信用户
     */
    @ApiOperation("删除微信用户")
    @PreAuthorize("@ss.hasPermi('citylife:user:remove')")
    @Log(title = "微信用户" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable String[] ids) {
        return toAjax(iCUserService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }
}
